package cn.com.dhc.danlu.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.dlware.global.DanluApplication;

/**
 * User: Geek_Soledad(wuhaiyang@danlu.com)
 * Date: 2016-08-07
 * Time: 10:42
 * Description: .....
 */
public class ToastUtil {
    @SuppressLint ("StaticFieldLeak")
    private static volatile ToastUtil sInstance;
    private static boolean isFirst = true;
    private Toast mToast;
    private Context context;

    private ToastUtil () {
        this.context = DanluApplication.get ();
    }

    public static ToastUtil getInstance () {
        if (null == sInstance) {
            synchronized (Object.class) {
                if (null == sInstance) {
                    sInstance = new ToastUtil ();
                }
            }
        }
        return sInstance;
    }

    public void showMobile () {
        show ("当前已切换到移动网络环境\n将为您加载压缩图片", "提示", true);
    }

    public void show (@NonNull String content, String title, boolean showCenter) {
        if (!isFirst) {
            View view = View.inflate (context, R.layout.view_toast_custom, null);
            TextView tvTitle = (TextView) view.findViewById (R.id.tv_title);
            if (TextUtils.isEmpty (title)) {
                tvTitle.setVisibility (View.GONE);
            } else {
                tvTitle.setVisibility (View.VISIBLE);
                tvTitle.setText (title);
            }
            ((TextView) view.findViewById (R.id.tv_content)).setText (content);

            Toast toast = Toast.makeText (context, content, Toast.LENGTH_SHORT);
            if (showCenter) {
                toast.setGravity (Gravity.CENTER, 0, 0);
            }
            toast.setView (view);
            toast.show ();
        }
        isFirst = false;
    }

    public void showWifi () {
        show ("当前已切换到WIFI网络环境\n将为您加载高清大图", "提示", true);
    }

    public void show (int tipsId) {
        if (null == mToast) {
            mToast = Toast.makeText (context, tipsId, Toast.LENGTH_SHORT);
        } else {
            mToast.setText (tipsId);
        }
        mToast.show ();
    }

    public void show (String tips) {
        if (null == mToast) {
            mToast = Toast.makeText (context, tips, Toast.LENGTH_SHORT);
        } else {
            mToast.setText (tips);
        }
        mToast.show ();
    }
}